<?php

require_once 'Pipes/Xml/Document.php';
require_once 'PipesImpl/Visitor.php';

class PipesCore_Helpers_Grid_HeaderBuilder implements PipesImpl_Visitor
{
    private $_document = null;
    
    public function __construct(Pipes_Xml_Document $document)
    {
        $this->_document = $document;
    }
    
    public function visit(PipesImpl_Visitable $visitable)
    {
        $meta = $visitable->meta;
        $table = $this->_document->getElementsByTagName('table')->item(0);
        
        $thead = $this->_document->createElement('thead');
        $table->appendChild($thead);
        
        $tr = $this->_document->createElement('tr');
        $thead->appendChild($tr);
        
        
        $properties = $meta->getColumns();
        foreach ($properties as $property) {            
            $th = $this->_document->createElement('th');

            $th->nodeValue = $property->label;
            
            if ($property->name == $meta->idColumn()->name) {
                $attribute = $this->_document->createAttribute('style');
                $attribute->nodeValue = 'display:none';
                $th->appendChild($attribute);

            }
            $tr->appendChild($th);
            unset($th);
        }

    }
}
